/**
 * Copyright(c) ${copyrightYear} ${company}
 * All rights reserved.
 * Created on ${date}
 */
package ${package}.util;
import java.io.File;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.servlet.ServletContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import ${package}.PropertyManager;

/**
 * 当前项目有关的工具类
 * 
 * @author ${author}
 */
public final class ProjectUtil {

    /**
     * spring上下文
     */
    private static WebApplicationContext applicationContext;

    /**
     * 默认构造方法
     */
    private ProjectUtil(){

    }

    /**
     * 初始化spring
     * 
     * @param servletContext
     */
    public static void initSpringContext(ServletContext servletContext) {
        WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        applicationContext = wac;
    }

    /**
     * 获取bean
     * 
     * @param key
     * @return
     */
    public static Object getSpringBean(String key) {
        return applicationContext.getBean(key);
    }
    
    /**
     * 获取EntityManager对象
     * 
     * @return
     */
    public static EntityManager getEntityManager() {
        return ((EntityManagerFactory) getSpringBean("entityManagerFactory")).createEntityManager();
    }
    /**
     * 获取文件的HOME目录
     * 
     * @return
     */
    public static File getHomeDir() {
        return new File(PropertyManager.getInstance().getXMLProperty(
                        PropertyManager.XML_${artifactId.toUpperCase()}_${artifactId.toUpperCase()}HOME));
    }


    /**
     * 获取文件的Tmp目录
     * 
     * @return
     */
    public static File getTmpDir() {
        return new File(PropertyManager.getInstance().getXMLProperty(
                        PropertyManager.XML_${artifactId.toUpperCase()}_${artifactId.toUpperCase()}HOME)
                        + File.separator + "tmp");
    }
}
